//commmon.js
$(function(){
	index_btn_operate();
	main_btn_operate();
	
})

function main_btn_operate()
{
	//1.排序
	$('.hy-js-order').blur(function(){
		    var _this=$(this);
			var g = (hungry.util._get('g'));
	        var	m = (hungry.util._get('m'));
	        var a=  _this.attr('data-action');
	        var id= _this.attr('data-id');
	        if (typeof(a) == 'undefined') {
		            a = 'ordering';
	        }

	if ($(_this).next('img').length == 0) {
		$(_this).parent().append('<img src="/ws/Admin/Webroot/images/loading.gif"/>')
	} else {
		$(_this).next('img').attr('src', '/ws/Admin/Webroot/images/loading.gif').show();
	}

	$.post('index.php?g=' + g + '&m=' + m + '&a=' + a, {
		val: _this.val(),
		id: id
	}, function(result) {

		if (result.error == 0) {
			$(_this).next('img').attr('src', '/ws/Admin/Webroot/images/yes.gif');


		} else {
			$(_this).next('img').attr('src', '/ws/Admin/Webroot/images/no.gif');
		}
	}, 'json');
	});
	//2.跳转页面
	$('.hy-js-href').click(function(){
		var _this=$(this);
		if(typeof(_this.attr('data-href'))!='undefined')
		{
			var id=_this.attr('data-id');
			if(typeof(id)=='undefined')
			{
				alert('data-id不存在');return;
			}
			parent.$('.hy-tabs li').removeClass('am-active');
			
			var tab_exist=0;
			parent.$('.hy-tabs li').each(function(){
				if($(this).attr('data-id')==id)
				{
					$(this).addClass('am-active');
					tab_exist=1;
				}
				
			})
			
			if(tab_exist==0)
			{
				parent.$('.hy-js-pageloading').show();
				if(typeof(_this.attr('data-title'))!='undefined')
		      {
		      	var tab_title=_this.attr('data-title');
		      }
		      else
		      {
		      	var tab_title=_this.html();
		      }
				
			var tab='<li data-id="'+id+'" class="am-active"><a  href="javascript:;"><span class="hy-tabs-title">'+tab_title+'</span><span class="am-icon-close"></span></a></li>'
			parent.$('.hy-tabs .am-nav').append(tab);
			}
			if(parent.$('#'+id).length==0)
			{
			parent.$('.hy-iframe iframe').hide();
			var iframe='<iframe onload="hy_loading()" id="'+id+'" src="'+_this.attr('data-href')+'" width="100%" height="100%"></iframe>';
			parent.$('.hy-iframe').append(iframe);
			}
			else
			{
				parent.$('.hy-iframe iframe').hide();
				parent.$('#'+id).show();
			}
		}
	});
	//3.单个删除
	$('.hy-js-del').live('click',function(e){
		e.preventDefault();
		var cfg=$(this).attr('data-param');
		cfg=eval('('+cfg+')');
	//	var url=typeof(cfg.aaa)=='undefined'?'123':cfg.aaa;
	var url='';
	var hide='';
	var parent='';
	for(i in cfg)
	{
		if(i=='url')
		{
			url=cfg[i];
		}
		else if(i=='hide')
		{
			hide=cfg[i];
		}
	}
			if (window.confirm('确定要删除吗？')) {
				var _this=$(this);
				
				if(url=='')
				{
					if(hide!='')
				  {
					   for(i=0;i<hide;i++)
					  {
						parent+='.parent()';
					  }
					  eval('_this'+parent+'.remove()');
				   }
				}
				else
				{
					$.post(url, {}, function(result) {
				   if (result.error > 0) {
					showMsg(result.content,-1);
				   } else {
					if(hide!='')
					{
					   for(i=0;i<hide;i++)
					  {
						parent+='.parent()';
					  }
					  eval('_this'+parent+'.remove()');
				    }
				showMsg(result.content,0);
				   }
			      }, 'json');
				}
			
		}
	});
	//4.搜索
	$('.hy-js-searchbtn').click(function(){
		
		hy_sort('k',$('.hy-js-search').val())
		});
	$('.hy-js-searchbtn-wen').click(function(){
		var param=new Array("c","k");
		var val=new Array($('.hy-js-search-type').val(),$('.hy-js-search').val());
		hy_sort_wen(param,val);
	});
	//5.筛选分类
	$('.hy-js-cat').change(function(){hy_sort('c',$(this).val())});
	//6.全选
	$('.hy-js-selectall').click(function()
	{ 
		if ($(this).attr('checked') == 'checked') {
		$("tbody .hy-js-id").each(function(index, element) {

			this.checked = true;
		});


	} else {
		$("tbody .hy-js-id").each(function(index, element) {

			$(this).removeAttr("checked");
		});

	}
	})
	//7.删除全部
	$('.hy-js-delall').click(function(){
		var action_name=$(this).attr('data-action');
	var a = typeof(action_name) == 'undefined' ? 'del' : action_name;
	var m = hungry.util._get('m');
	var g = hungry.util._get('g');
	var check_arr = new Array;
	if (window.confirm('确定要删除全部吗？')) {
		$("tbody .hy-js-id").each(function(index, element) {

			if ($(this).attr('checked') == 'checked') {

				check_arr.push($(this).val())
				$(this).parent().parent().remove();
			}
		});
		
		$.post('index.php?g=' + g + '&m=' + m + '&a=' + a, {
			id: check_arr
		}, function(result) {
			if (result.error == 0) {
				showMsg(result.content,0);
				
			}else
			{
				showMsg(result.content,-1);
			}
		}, 'json');

	}
		
	});
	//8.编辑相片集
	$('.hy-js-img').click(function(e){
		e.preventDefault();
		var url=$(this).attr('data-href');
		hy_winOpen(url,{height:'70%',width:'80%'})
	});
	//9.ajax跳转
	$('.hy-js-ajax').click(function(){
		var url=$(this).attr('data-href');
		if(typeof($(this).attr('data-confirm'))!='undefined')
		{
			if(typeof($(this).attr('data-word'))!='undefined')
		  {
			var word=$(this).attr('data-word');
		  }
		  else
		  {
		  	var word='你确定要进行此操作吗？';
		  }
			if(!confirm(word))
			{
				return;
			}
		}
		$.post(url, {}, function(result) {

			if (result.error == 0) {
				
				showMsg(result.content,0);
				
			}else
			{
				
				showMsg(result.content,0);
				if(typeof result.url !='undefined'&&result.url!=""&&result.url!=null){
					window.location.href=result.url;
				}
			}
		}, 'json');
	});
	//10.打开新窗口
	$('.hy-js-window').click(function(e){
		e.preventDefault();
		var url=$(this).attr('data-href');
		var cfg=$(this).attr('data-cfg');
		cfg=eval('('+cfg+')');
	//	var url=typeof(cfg.aaa)=='undefined'?'123':cfg.aaa;
	var height='70%';
	var width='80%';
	for(i in cfg)
	{
		if(i=='h')
		{
			height=cfg[i];
		}
		else if(i=='w')
		{
			width=cfg[i];
		}
	}
		hy_winOpen(url,{height:height,width:width})
	});
	//11.切换隐藏模块
	$('.hy-js-menutab .hy-js-menubtn').click(function() {
					$t = $(this).attr('data-target');
					$('.hy-js-menupanel').hide();
					$($t).show();
					if($($t + ' input').length>0)
					{
						$($t + ' input').focus();
					}
					else if($($t + ' textarea').length>0)
					{
						$($t + ' textarea').focus();
					}
					else if($($t + ' select').length>0)
					{
						$($t + ' select').focus();
					}
					
				});
	//12.关闭提示
	$('.hy-js-closealert').live('click',function(){
		$('.hy-alert').fadeOut();
	})
	//13栏目删除弹出框
	$('.hy-js-delmenu').click(function(){
		if(parent.$('.hy-delmenu').length>0)
		{
			parent.$('.hy-delmenu').show;
		}
		else
		{
			parent.$('body').append('');
		}
	});
	//14删除dom元素
	$('.hy-js-deldom').live('click',function()
	{
		var _this=$(this);
		var hide=$(this).attr('data-hide');
		var parent='';
		if(hide)
		{
			for(i=0;i<hide;i++)
	      {
		     parent+='.parent()';
		 }
		
		}
		eval('_this'+parent+'.remove()');
	})
		//14输入空格的时候自动切换成逗号
		$('.hy-js-explode').live('keyup',function(e){
			
			var _this=$(this);
			var flag=_this.attr('hy-pause');
			if(event.keyCode==32&&flag!='1')
			{
				var val=_this.val();
				val=val.substr(0,val.length-1);
				_this.val(val+',');
				_this.attr('hy-pause',1);
				setTimeout(function(){
					_this.attr('hy-pause',0);
				},1000)
			}
		});

}
function index_btn_operate()
{
	$('.hy-tabs .hy-tabs-title').live('click',function(){
		
		var _this=$(this).parent().parent();
		$('.hy-tabs li').removeClass('am-active');
		if(this.className!='am-active')
		{
			_this.addClass('am-active');
			var id=_this.attr('data-id');
			$('.hy-iframe iframe').hide();
			$('#'+id).show();
		}
	});
	
	$('.hy-tabs .hy-tabs-title').live("contextmenu",function(e){
                if(typeof e.preventDefault === "function"){
                        e.preventDefault();
                        e.stopPropagation();
                }else{
                        e.returnValue = false;
                        e.cancelBubble = true;
                }
                var _this=$(this).parent().parent();
                var id=_this.attr('data-id');
                var title=$(this).html();
                $('.hy-js-refresh').unbind('click');
                 $('.hy-js-addfavor').unbind('click');
                 
                 $('.hy-js-refresh').click(function(){
                 	$('#'+id).attr('src', $('#'+id).attr('src'));
                 	$('.hy-tab-contextmenu').hide();
                 })
                 //收藏
                 $('.hy-js-addfavor').click(function(){
                 	var btn=$(this);
                 	btn.css('color','#ccc');
                 	var url=$('#'+id).attr('src');
                 	$.post('index.php?g=admin&m=index&a=addfavor',{data_id:id,url:url,name:title},function(result){
                 		btn.css('color','');
                 		alert(result.content);
                 		$('.hy-tab-contextmenu').hide();
                 	},'json')
                 })
                $('.hy-tab-contextmenu').css({"left":e.clientX,"top":e.clientY}).fadeIn();
               // $(this).addClass("an").siblings("li").removeClass("an")
               // $("#menu").css({"left":e.clientX,"top":e.clientY}).fadeIn()
        })
	//点击则隐藏
	 $(document).bind("click",function(e){
                var target  = $(e.target);
                if(target.closest(".hy-tab-contextmenu").length == 0){
                         $('.hy-tab-contextmenu').hide();
                         parent.$('.hy-tab-contextmenu').hide();
                }
        })
	$('.hy-tabs .am-icon-close').live('click',function(){
		var _this=$(this);
		var tab_li=_this.parent().parent();
		
		if(tab_li.hasClass('am-active'))
		{
			if(tab_li.prev().length>0)
			{
				tab_li.prev().addClass('am-active');
				var id=tab_li.prev().attr('data-id');
		        $('#'+id).show();
			}
			else if(tab_li.next().length>0)
			{
				tab_li.next().addClass('am-active');
				var id=tab_li.next().attr('data-id');
		        $('#'+id).show();
			}
			else
			{
				var tab='<li data-id="index" class="am-active"><a  href="javascript:;"><span class="hy-tabs-title">首页</span><span class="am-icon-close"></span></a></li>'
			    $('.hy-tabs .am-nav').append(tab);
			    var iframe='<iframe onload="javascript:hy_loading()" id="index" src="index.php?g=admin&m=index&a=main" width="100%" height="100%"></iframe>';
			    $('.hy-iframe').append(iframe);
			}
			
		}
		
		tab_li.remove();
		var id=tab_li.attr('data-id');
		$('#'+id).remove();
		
		
		
	});
	
	$('.hy-sidebar a').click(function(){
		
		var _this=$(this);
		if(typeof(_this.attr('data-href'))!='undefined')
		{
			var id=_this.attr('data-id');
			if(typeof(id)=='undefined')
			{
				alert('data-id不存在');
			}
			$('.hy-tabs li').removeClass('am-active');
			
			var tab_exist=0;
			$('.hy-tabs li').each(function(){
				if($(this).attr('data-id')==id)
				{
					$(this).addClass('am-active');
					tab_exist=1;
				}
				
			})
			if(tab_exist==0)
			{
				$('.hy-js-pageloading').show();
			var tab='<li data-id="'+id+'" class="am-active"><a  href="javascript:;"><span class="hy-tabs-title">'+_this.html()+'</span><span class="am-icon-close"></span></a></li>'
			$('.hy-tabs .am-nav').append(tab);
			}
			
			if($('#'+id).length==0)
			{
			$('.hy-iframe iframe').hide();
			var iframe='<iframe onload="javascript:hy_loading()" id="'+id+'" src="'+_this.attr('data-href')+'" width="100%" height="100%"></iframe>';
			$('.hy-iframe').append(iframe);
			}
			else
			{
				$('.hy-iframe iframe').hide();
				$('#'+id).show();
			}
		}
	});
	
	$('.hy-win-close').live('click',function(){
		$('.hy-mask').hide();
	   $('#hy-win-container').remove();
	})
	
}
//页面加载完毕
function hy_loading()
{
	$('.hy-js-pageloading').fadeOut(300);
}
//筛选
function hy_sort(param, val) {
	var url = window.location.href;
	var arrStr = url.substring(url.indexOf("?") + 1).split("&");
	//return arrStr;

	var new_url = '';
	for (var i = 1; i < arrStr.length; i++) {
		var loc = arrStr[i].indexOf(param + "=");
		if (loc != -1) {

		} else {
			new_url += '&' + arrStr[i];
		}

	}
	
	
	if (val != '' && val != 0) {
		window.location.href = '/?' + arrStr[0] + new_url + '&' + param + '=' + val;
	} else {
       window.location.href = '/?' + arrStr[0] + new_url;
	}

}

function hy_sort_wen(param, val) {
	var url = window.location.href;
	var arrStr = url.substring(url.indexOf("?") + 1).split("&");

	var new_url = '';
	for (var i = 1; i < arrStr.length; i++) {
		var loc = arrStr[i].indexOf(param[0] + "=");
		var loc2 = arrStr[i].indexOf(param[1] + "=");
		if (loc != -1||loc2 != -1) {

		}else {
			new_url += '&' + arrStr[i];
		}

	}
	if (val[1] != '' && val[1] != 0 && param != 0) {
		window.location.href = '/ws/index.php?' + arrStr[0] + new_url + '&' + param[0] + '=' + val[0]+ '&' + param[1] + '=' + val[1];
	} else {
       window.location.href = '/ws/index.php?' + arrStr[0] + new_url;
	}

}
function hy_cutimg(cfg,_callback)
{
	var btn=typeof(cfg.target)=='undefined'?'.hy-js-cutimg':cfg.target;
	
	$(btn).live('click',function(e){
		e.preventDefault();
		var _this=$(this);
			if(typeof(_this.attr('data-src'))!='undefined')
			{
				var img=_this.attr('data-src');
			}
			else
			{
				var img=_this.attr('src')
			}
		
		//点击保存后的回调方法
		var save=function(_iframe){
			var chk_value ='';  
    		var _parent=_iframe.parents('body');
    		
    		chk_value=_parent.find('#thumb').val();
    		
    	   _callback(chk_value,_this);
		}
		hy_winOpen('index.php?g=admin&m=index&a=cutimg&i='+img,{height:'80%',width:'90%',save:save,_this:_this})
	});
}
function hy_uploadimg(url,cfg,_callback)
{
	var btn=typeof(cfg.target)=='undefined'?'.hy-js-uploadimg':cfg.target;
	
	$(btn).live('click',function(e){
		e.preventDefault();
		var _this=$(this);
		//点击保存后的回调方法
		var save=function(_iframe){
			var chk_value =[];  
    		var _parent=_iframe.parents('body');
    		
    		_parent.find('input[name=uploadimg]:checked').each(function(){
    			 chk_value.push($(this).val());    
    		})
    		
    	   _callback(chk_value,_this);
		}
		hy_winOpen('index.php?g=admin&m=index&a=uploadimg&s='+url,{height:'70%',width:'50%',save:save,_this:_this})
	});
}
//提交表单
function hy_submit(url,args,_callback)
{
	var btn='.hy-js-submit';
	var form='.hy-js-form';
	var this_btn='';
	if(typeof(args)!='undefined')
	{
		for(i in args)
		{
			if(i==btn)
			{
				btn=args.btn;
			}
			else if(i==form)
			{
				form=args.form;
			}
		}
		
	}
	
	$(btn).click(function(e){
		               this_btn=$(this);
		               if(this_btn.attr('hy-disabled'))
		               {
		               	  return;
		               }
		               this_btn.attr('hy-disabled',1)
		               this_btn.removeClass('am-btn-primary');
		                
					    var data={};
						e.preventDefault();
						var data_arr=$(form).serialize().replace(/\+/g,' ').split('&');

                        for(var i=0;i<data_arr.length;i++)
						{
							var temp=data_arr[i].split('=');
							if(typeof(data[hungry.util.utf8_decode(temp[0])])!='undefined')
							{
								var arr=[];
								if(data[hungry.util.utf8_decode(temp[0])] instanceof Array)
							  {
								arr=data[hungry.util.utf8_decode(temp[0])];
							  }
							  else
							  {
								    arr.push(data[hungry.util.utf8_decode(temp[0])]);
							  }
							  	arr.push(hungry.util.utf8_decode(temp[1]));
							
								data[hungry.util.utf8_decode(temp[0])]=arr;
								//data[hungry.util.utf8_decode(temp[0])].push(hungry.util.utf8_decode(temp[1]));
						    
							}
							else
							{
							data[hungry.util.utf8_decode(temp[0])]=hungry.util.utf8_decode(temp[1]);
							}
						}

					 $.post(url,data,function(result){
					 	this_btn.addClass('am-btn-primary');
					 	this_btn.removeAttr('hy-disabled');
					 	    if(typeof(_callback)!='undefined')
					 		{
					 			_callback(result,{form:$(form),btn:this_btn});
					 			return;
					 		}
					 		
					 	//alert(result);return;
//					 	if(result.error==0)
//					 	{
//					 		if(typeof(_callback)!='undefined')
//					 		{
//					 			_callback();
//					 		}
//					 	}
					 	showMsg(result.content,result.error);
					 },'json')
					
				})
}
//打卡新窗口
function hy_winOpen(url,cfg)
{
	var h=typeof(cfg.height)=='undefined'?'':cfg.height;
	var w=typeof(cfg.width)=='undefined'?'':cfg.width;
	var _this=typeof(cfg._this)=='undefined'?'':cfg._this;
	var callback=typeof(cfg.callback)=='undefined'?'':cfg.callback;
	
	var html="<div id='hy-win-container' style='width:"+w+";height:"+h+"' class='hy-win-container'>";
	html+="<div class='hy-win-head'><a href='javascript:;' class='hy-win-close' >X</a></div>";
	html+='<div class="am-alert hy-js-pageloading am-alert-warning"><strong>信息：</strong>页面加载中......<i class="am-icon-refresh am-icon-spin"></i></div>';
	html+="<iframe scrolling='yes'  onload='hy_loading()' name='uploadimg-iframe' id='uploadimg-iframe' width='100%' height='100%'  src='"+url+"'></iframe>";
	html+="<div class='hy-win-foot'></div>";
	html+="</div>";
	parent.$('.hy-mask').show();
	parent.$('body').append(html);
	var win_h=parent.$('#hy-win-container').height()/2;
	var win_w=parent.$('#hy-win-container').width()/2;
    parent.$('#hy-win-container').css('margin-left','-'+win_w+'px');
    parent.$('#hy-win-container').css('margin-top','-'+win_h+'px');
    if(typeof(cfg.save)!='undefined')
    {
    	parent.$('#hy-win-container iframe').load(function(){
    		$(this).contents().find(".hy-js-save").click(function(){
    	    result=cfg.save($(this));
    		parent.$('.hy-mask').hide();
	        parent.$('#hy-win-container').remove();
    	});
    	})
    }
    if(typeof(cfg.save2)!='undefined')
    {
    	parent.$('#hy-win-container iframe').load(function(){
    		$(this).contents().find(".hy-js-save2").click(function(){
    	    result=cfg.save($(this));
    		parent.$('.hy-mask').hide();
	        parent.$('#hy-win-container').remove();
    	});
    	})
    }
}
//显示信息
function showMsg(content,type)
	{
		var check = 0;
		if(type == 0)
		{
			var html='<div class="am-alert am-alert-success"><strong>信息：</strong>'+content+'<a href="javascript:;" class="am-icon-close am-fr hy-js-closealert"></a></div>';
		}
		else if(type == '-1')
		{
			var html='<div class="am-alert am-alert-success"><strong>信息：</strong>'+content+'<a href="javascript:;" class="am-icon-close am-fr hy-js-closealert"></a></div>';
			check = 1;
		}
		else
		{
			var html='<div class="am-alert am-alert-danger"><strong>注意：</strong>'+content+'<a href="javascript:;" class="am-icon-close am-fr hy-js-closealert"></a></div>';
		}
		$('.hy-alert').hide().html(html).fadeIn();
		setTimeout(function(){$('.hy-alert').fadeOut()},3000);
		if(check != 0)
		{
			location.reload();
		}
	}
